﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace SetIEProxy
{
    public partial class Option : Form
    {
        #region Attributes

        QT.IO.INIFile ProxyFile = new QT.IO.INIFile(Application.StartupPath + @"\Setting.ini");

        #endregion

        #region Form

        public Option()
        {
            InitializeComponent();
            InitOption();
        }

        private void Option_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!File.Exists(GlobalAttrib.IniPath))
            {
                MessageBox.Show("代理服务器未设置，程序退出");
                Application.ExitThread();
            }
        }

        #endregion

        #region Button

        private void btnSaveOption_Click(object sender, EventArgs e)
        {
            #region 检查电信代理配置

            if (this.cbTelCom.Checked)
            {
                if (string.IsNullOrEmpty(this.tbGroup1.Text))
                {
                    MessageBox.Show("请输入名称");
                    return;
                }
                if (this.tbTelIP.Text.Length == 0)
                {
                    MessageBox.Show("请输入代理服务器地址");
                    return;
                }
                if (this.tbTelPort.Text.Length == 0)
                {
                    MessageBox.Show("请输入代理服务器端口");
                    return;
                }
                if (!QT.Text.Match.IsNumeric(this.tbTelPort.Text) && int.Parse(this.tbTelPort.Text) > 0 && int.Parse(this.tbTelPort.Text) < 65536)
                {
                    MessageBox.Show("请输入合法的端口号");
                    return;
                }

                ProxyFile.WriteInivalue("TelCom", "Name", this.tbGroup1.Text);
                ProxyFile.WriteInivalue("TelCom", "Enable", "1");
                ProxyFile.WriteInivalue("TelCom", "Server", this.tbTelIP.Text);
                ProxyFile.WriteInivalue("TelCom", "Port", this.tbTelPort.Text);
            }
            else
            {
                ProxyFile.WriteInivalue("TelCom", "Enable", "0");
            }

            #endregion

            #region 检查联通代理配置

            if (this.cbUniCom.Checked)
            {
                if (string.IsNullOrEmpty(this.tbGroup2.Text))
                {
                    MessageBox.Show("请输入名称");
                    return;
                }
                if (this.tbUniIP.Text.Length == 0)
                {
                    MessageBox.Show("请输入代理服务器地址");
                    return;
                }
                if (this.tbUniPort.Text.Length == 0)
                {
                    MessageBox.Show("请输入代理服务器端口");
                    return;
                }
                if (!QT.Text.Match.IsNumeric(this.tbUniPort.Text) && int.Parse(this.tbUniPort.Text) > 0 && int.Parse(this.tbUniPort.Text) < 65536)
                {
                    MessageBox.Show("请输入合法的端口号");
                    return;
                }

                ProxyFile.WriteInivalue("UniCom", "Name", this.tbGroup2.Text);
                ProxyFile.WriteInivalue("UniCom", "Enable", "1");
                ProxyFile.WriteInivalue("UniCom", "Server", this.tbUniIP.Text);
                ProxyFile.WriteInivalue("UniCom", "Port", this.tbUniPort.Text);
            }
            else
            {
                ProxyFile.WriteInivalue("UniCom", "Enable", "0");
            }

            #endregion

            #region 检查过滤配置

            ProxyFile.WriteInivalue("Filter", "Enable", this.cbFilter.Checked ? "1" : "0");
            ProxyFile.WriteInivalue("Filter", "Regular", this.tbRegular.Text);
            GlobalAttrib.Filters = this.tbRegular.Text;

            #endregion

            this.Close();
        }

        #endregion

        #region CheckBox

        private void cbTelCom_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbTelCom.Checked)
            {
                this.gbTelCom.Enabled = true;
                this.tbGroup1.Enabled = true;
            }
            else
            {
                this.gbTelCom.Enabled = false;
                this.tbGroup1.Enabled = false;
            }
        }

        private void cbUniCom_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbUniCom.Checked)
            {
                this.gbUniCom.Enabled = true;
                this.tbGroup2.Enabled = true;
            }
            else
            {
                this.gbUniCom.Enabled = false;
                this.tbGroup2.Enabled = false;
            }
        }

        private void cbFilter_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbFilter.Checked)
            {
                this.gbFilter.Enabled = true;
            }
            else
            {
                this.gbFilter.Enabled = false;
            }
        }

        #endregion

        #region Method

        private void InitOption()
        {
            string TelComEn;
            string UniComEn;
            string FilterEn;
            if (!ProxyFile.ReadInivalue("TelCom", "Enable", out TelComEn))
            {
                TelComEn = "0";
            }
            if (!ProxyFile.ReadInivalue("UniCom", "Enable", out UniComEn))
            {
                UniComEn = "0";
            }
            if (!ProxyFile.ReadInivalue("Filter", "Enable", out FilterEn))
            {
                FilterEn = "0";
            }

            //初始化电信设置
            if (TelComEn == "1")
            {
                this.cbTelCom.Checked = true;
                this.gbTelCom.Enabled = true;
            }
            else
            {
                this.cbTelCom.Checked = false;
                this.gbTelCom.Enabled = false;
            }
            string TelComIP;
            string TelComPort;
            string TelComName;
            if (ProxyFile.ReadInivalue("TelCom", "Name", out TelComName))
            {
                this.tbGroup1.Text = TelComName;
            }
            if (ProxyFile.ReadInivalue("TelCom", "Server", out TelComIP))
            {
                this.tbTelIP.Text = TelComIP;
            }
            if (ProxyFile.ReadInivalue("TelCom", "Port", out TelComPort))
            {
                this.tbTelPort.Text = TelComPort;
            }
            cbTelCom_CheckedChanged(new object(), new EventArgs());

            //初始化联通设置
            if (UniComEn == "1")
            {
                this.cbUniCom.Checked = true;
                this.gbUniCom.Enabled = true;
            }
            else
            {
                this.cbUniCom.Checked = false;
                this.gbUniCom.Enabled = false;
            }
            string UniComIP;
            string UniComPort;
            string UniComName;
            if (ProxyFile.ReadInivalue("UniCom", "Name", out UniComName))
            {
                this.tbGroup2.Text = UniComName;
            }
            if (ProxyFile.ReadInivalue("UniCom", "Server", out UniComIP))
            {
                this.tbUniIP.Text = UniComIP;
            }
            if (ProxyFile.ReadInivalue("UniCom", "Port", out UniComPort))
            {
                this.tbUniPort.Text = UniComPort;
            }
            cbUniCom_CheckedChanged(new object(), new EventArgs());

            //初始化过滤设置
            if (FilterEn == "1") this.cbFilter.Checked = true;
            string Regular;
            if (ProxyFile.ReadInivalue("Filter", "Regular", out Regular))
            {
                this.tbRegular.Text = Regular;
            }
            cbFilter_CheckedChanged(new object(), new EventArgs());
        }

        #endregion
    }
}
